{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#importing required librarires\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "from datetime import datetime"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "#importing metrics libraries to calculate error, score etc.\n",
    "from sklearn.metrics import mean_absolute_error as mae, mean_squared_error as mse, accuracy_score, r2_score\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>mid</th>\n",
       "      <th>date</th>\n",
       "      <th>venue</th>\n",
       "      <th>bat_team</th>\n",
       "      <th>bowl_team</th>\n",
       "      <th>batsman</th>\n",
       "      <th>bowler</th>\n",
       "      <th>runs</th>\n",
       "      <th>wickets</th>\n",
       "      <th>overs</th>\n",
       "      <th>runs_last_5</th>\n",
       "      <th>wickets_last_5</th>\n",
       "      <th>striker</th>\n",
       "      <th>non-striker</th>\n",
       "      <th>total</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2008-04-18</td>\n",
       "      <td>M Chinnaswamy Stadium</td>\n",
       "      <td>Kolkata Knight Riders</td>\n",
       "      <td>Royal Challengers Bangalore</td>\n",
       "      <td>SC Ganguly</td>\n",
       "      <td>P Kumar</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0.1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>222</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>2008-04-18</td>\n",
       "      <td>M Chinnaswamy Stadium</td>\n",
       "      <td>Kolkata Knight Riders</td>\n",
       "      <td>Royal Challengers Bangalore</td>\n",
       "      <td>BB McCullum</td>\n",
       "      <td>P Kumar</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0.2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>222</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>2008-04-18</td>\n",
       "      <td>M Chinnaswamy Stadium</td>\n",
       "      <td>Kolkata Knight Riders</td>\n",
       "      <td>Royal Challengers Bangalore</td>\n",
       "      <td>BB McCullum</td>\n",
       "      <td>P Kumar</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0.2</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>222</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>2008-04-18</td>\n",
       "      <td>M Chinnaswamy Stadium</td>\n",
       "      <td>Kolkata Knight Riders</td>\n",
       "      <td>Royal Challengers Bangalore</td>\n",
       "      <td>BB McCullum</td>\n",
       "      <td>P Kumar</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0.3</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>222</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>2008-04-18</td>\n",
       "      <td>M Chinnaswamy Stadium</td>\n",
       "      <td>Kolkata Knight Riders</td>\n",
       "      <td>Royal Challengers Bangalore</td>\n",
       "      <td>BB McCullum</td>\n",
       "      <td>P Kumar</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0.4</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>222</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   mid        date                  venue               bat_team  \\\n",
       "0    1  2008-04-18  M Chinnaswamy Stadium  Kolkata Knight Riders   \n",
       "1    1  2008-04-18  M Chinnaswamy Stadium  Kolkata Knight Riders   \n",
       "2    1  2008-04-18  M Chinnaswamy Stadium  Kolkata Knight Riders   \n",
       "3    1  2008-04-18  M Chinnaswamy Stadium  Kolkata Knight Riders   \n",
       "4    1  2008-04-18  M Chinnaswamy Stadium  Kolkata Knight Riders   \n",
       "\n",
       "                     bowl_team      batsman   bowler  runs  wickets  overs  \\\n",
       "0  Royal Challengers Bangalore   SC Ganguly  P Kumar     1        0    0.1   \n",
       "1  Royal Challengers Bangalore  BB McCullum  P Kumar     1        0    0.2   \n",
       "2  Royal Challengers Bangalore  BB McCullum  P Kumar     2        0    0.2   \n",
       "3  Royal Challengers Bangalore  BB McCullum  P Kumar     2        0    0.3   \n",
       "4  Royal Challengers Bangalore  BB McCullum  P Kumar     2        0    0.4   \n",
       "\n",
       "   runs_last_5  wickets_last_5  striker  non-striker  total  \n",
       "0            1               0        0            0    222  \n",
       "1            1               0        0            0    222  \n",
       "2            2               0        0            0    222  \n",
       "3            2               0        0            0    222  \n",
       "4            2               0        0            0    222  "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#importing our dataset\n",
    "df=pd.read_csv('ipl.csv')\n",
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['mid', 'date', 'venue', 'bat_team', 'bowl_team', 'batsman', 'bowler',\n",
       "       'runs', 'wickets', 'overs', 'runs_last_5', 'wickets_last_5', 'striker',\n",
       "       'non-striker', 'total'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#checking all columns\n",
    "df.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "mid                 int64\n",
       "date               object\n",
       "venue              object\n",
       "bat_team           object\n",
       "bowl_team          object\n",
       "batsman            object\n",
       "bowler             object\n",
       "runs                int64\n",
       "wickets             int64\n",
       "overs             float64\n",
       "runs_last_5         int64\n",
       "wickets_last_5      int64\n",
       "striker             int64\n",
       "non-striker         int64\n",
       "total               int64\n",
       "dtype: object"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#checking datatypes of columns\n",
    "df.dtypes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "mid               0\n",
       "date              0\n",
       "venue             0\n",
       "bat_team          0\n",
       "bowl_team         0\n",
       "batsman           0\n",
       "bowler            0\n",
       "runs              0\n",
       "wickets           0\n",
       "overs             0\n",
       "runs_last_5       0\n",
       "wickets_last_5    0\n",
       "striker           0\n",
       "non-striker       0\n",
       "total             0\n",
       "dtype: int64"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#checking if any null value is present\n",
    "df.isnull().sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  1,   2,   3,   4,   5,   6,   7,   8,   9,  10,  11,  12,  13,\n",
       "        14,  15,  16,  17,  18,  19,  20,  21,  22,  23,  24,  25,  26,\n",
       "        27,  28,  29,  30,  31,  32,  33,  34,  35,  36,  37,  38,  39,\n",
       "        40,  41,  42,  43,  44,  45,  46,  47,  48,  49,  50,  51,  52,\n",
       "        53,  54,  55,  56,  57,  58,  59,  60,  61,  62,  63,  64,  65,\n",
       "        66,  67,  68,  69,  70,  71,  72,  73,  74,  75,  76,  77,  78,\n",
       "        79,  80,  81,  82,  83,  84,  85,  86,  87,  88,  89,  90,  91,\n",
       "        92,  93,  94,  95,  96,  97,  98,  99, 100, 101, 102, 103, 104,\n",
       "       105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117,\n",
       "       118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130,\n",
       "       131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143,\n",
       "       144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156,\n",
       "       157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169,\n",
       "       170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182,\n",
       "       183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195,\n",
       "       196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208,\n",
       "       209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221,\n",
       "       222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234,\n",
       "       235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247,\n",
       "       248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259, 260,\n",
       "       261, 262, 263, 264, 265, 266, 267, 268, 269, 270, 271, 272, 273,\n",
       "       274, 275, 276, 277, 278, 279, 280, 281, 282, 283, 284, 285, 286,\n",
       "       287, 288, 289, 290, 291, 292, 293, 294, 295, 296, 297, 298, 299,\n",
       "       300, 301, 302, 303, 304, 305, 306, 307, 308, 309, 310, 311, 312,\n",
       "       313, 314, 315, 316, 317, 318, 319, 320, 321, 322, 323, 324, 325,\n",
       "       326, 327, 328, 329, 330, 331, 332, 333, 334, 335, 336, 337, 338,\n",
       "       339, 340, 341, 342, 343, 344, 345, 346, 347, 348, 349, 350, 351,\n",
       "       352, 353, 354, 355, 356, 357, 358, 359, 360, 361, 362, 363, 364,\n",
       "       365, 366, 367, 368, 369, 370, 371, 372, 373, 374, 375, 376, 377,\n",
       "       378, 379, 380, 381, 382, 383, 384, 385, 386, 387, 388, 389, 390,\n",
       "       391, 392, 393, 394, 395, 396, 397, 398, 399, 400, 401, 402, 403,\n",
       "       404, 405, 406, 407, 408, 409, 410, 411, 412, 413, 414, 415, 416,\n",
       "       417, 418, 419, 420, 421, 422, 423, 424, 425, 426, 427, 428, 429,\n",
       "       430, 431, 432, 433, 434, 435, 436, 437, 438, 439, 440, 441, 442,\n",
       "       443, 444, 445, 446, 447, 448, 449, 450, 451, 452, 453, 454, 455,\n",
       "       456, 457, 458, 459, 460, 461, 462, 463, 464, 465, 466, 467, 468,\n",
       "       469, 470, 471, 472, 473, 474, 475, 476, 477, 478, 479, 480, 481,\n",
       "       482, 483, 484, 485, 486, 487, 488, 489, 490, 491, 492, 493, 494,\n",
       "       495, 496, 497, 498, 499, 500, 501, 502, 503, 504, 505, 506, 507,\n",
       "       508, 509, 510, 511, 512, 513, 514, 515, 516, 517, 518, 519, 520,\n",
       "       521, 522, 523, 524, 525, 526, 527, 528, 529, 530, 531, 532, 533,\n",
       "       534, 535, 536, 537, 538, 539, 540, 541, 542, 543, 544, 545, 546,\n",
       "       547, 548, 549, 550, 551, 552, 553, 554, 555, 556, 557, 558, 559,\n",
       "       560, 561, 562, 563, 564, 565, 566, 567, 568, 569, 570, 571, 572,\n",
       "       573, 574, 575, 576, 577, 578, 579, 580, 581, 582, 583, 584, 585,\n",
       "       586, 587, 588, 589, 590, 591, 592, 593, 594, 595, 596, 597, 598,\n",
       "       599, 600, 601, 602, 603, 604, 605, 606, 607, 608, 609, 610, 611,\n",
       "       612, 613, 614, 615, 616, 617], dtype=int64)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# mid here represents match id. A useless column\n",
    "df['mid'].unique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(76014, 9)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#removing certain column that has least impact on prediction.\n",
    "#ps note: venue can be a deciding factor but after including venue, error was very large. So, drop it.\n",
    "columns_to_remove = ['mid','venue', 'batsman', 'bowler', 'striker', \n",
    "                    'non-striker']\n",
    "df.drop(labels=columns_to_remove, axis=1, inplace=True)\n",
    "df.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array(['Kolkata Knight Riders', 'Chennai Super Kings', 'Rajasthan Royals',\n",
       "       'Mumbai Indians', 'Deccan Chargers', 'Kings XI Punjab',\n",
       "       'Royal Challengers Bangalore', 'Delhi Daredevils',\n",
       "       'Kochi Tuskers Kerala', 'Pune Warriors', 'Sunrisers Hyderabad',\n",
       "       'Rising Pune Supergiants', 'Gujarat Lions',\n",
       "       'Rising Pune Supergiant'], dtype=object)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#checking all the teams playing\n",
    "df['bat_team'].unique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "#keeping only those teams that are currently playing\n",
    "valid_teams = ['Kolkata Knight Riders', 'Chennai Super Kings', 'Rajasthan Royals',\n",
    "       'Mumbai Indians','Kings XI Punjab',\n",
    "       'Royal Challengers Bangalore', 'Delhi Daredevils','Sunrisers Hyderabad']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(53811, 9)"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#eliminating non valid teams\n",
    "df=df[(df['bat_team'].isin(valid_teams))& (df['bowl_team'].isin(valid_teams))]\n",
    "df.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(40108, 9)"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#lower limit of over is kept as 5.\n",
    "df=df[df['overs']>=5.0]\n",
    "df.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dtype('<M8[ns]')"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#converting datatype of datecolumn from string to <M8[ns].\n",
    "df['date'] = df['date'].apply(lambda x: datetime.strptime(x, '%Y-%m-%d'))\n",
    "df['date'].dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['date', 'runs', 'wickets', 'overs', 'runs_last_5', 'wickets_last_5',\n",
       "       'total', 'bat_team_Chennai Super Kings', 'bat_team_Delhi Daredevils',\n",
       "       'bat_team_Kings XI Punjab', 'bat_team_Kolkata Knight Riders',\n",
       "       'bat_team_Mumbai Indians', 'bat_team_Rajasthan Royals',\n",
       "       'bat_team_Royal Challengers Bangalore', 'bat_team_Sunrisers Hyderabad',\n",
       "       'bowl_team_Chennai Super Kings', 'bowl_team_Delhi Daredevils',\n",
       "       'bowl_team_Kings XI Punjab', 'bowl_team_Kolkata Knight Riders',\n",
       "       'bowl_team_Mumbai Indians', 'bowl_team_Rajasthan Royals',\n",
       "       'bowl_team_Royal Challengers Bangalore',\n",
       "       'bowl_team_Sunrisers Hyderabad'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#handling categorical features\n",
    "new_df = pd.get_dummies(data=df, columns=[ 'bat_team', 'bowl_team'])\n",
    "new_df.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "#arranging all the columns.\n",
    "new_df= new_df[['date','bat_team_Chennai Super Kings', 'bat_team_Delhi Daredevils', 'bat_team_Kings XI Punjab',\n",
    "              'bat_team_Kolkata Knight Riders', 'bat_team_Mumbai Indians', 'bat_team_Rajasthan Royals',\n",
    "              'bat_team_Royal Challengers Bangalore', 'bat_team_Sunrisers Hyderabad',\n",
    "              'bowl_team_Chennai Super Kings', 'bowl_team_Delhi Daredevils', 'bowl_team_Kings XI Punjab',\n",
    "              'bowl_team_Kolkata Knight Riders', 'bowl_team_Mumbai Indians', 'bowl_team_Rajasthan Royals',\n",
    "              'bowl_team_Royal Challengers Bangalore', 'bowl_team_Sunrisers Hyderabad',\n",
    "              'overs', 'runs', 'wickets', 'runs_last_5', 'wickets_last_5', 'total']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "#splitting into train and test data.\n",
    "X_train=new_df.drop(labels='total', axis=1)[new_df['date'].dt.year<=2016]\n",
    "X_test=new_df.drop(labels='total', axis=1)[new_df['date'].dt.year>=2017]\n",
    "\n",
    "y_train=new_df[new_df['date'].dt.year<=2016]['total'].values\n",
    "y_test=new_df[new_df['date'].dt.year>=2017]['total'].values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "#now date is no longer required so dropping it.\n",
    "X_train.drop(labels='date',axis=True,inplace=True)\n",
    "X_test.drop(labels='date',axis=True,inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((37330, 21), (2778, 21), (37330,), (2778,))"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_train.shape,X_test.shape,y_train.shape,y_test.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Performing different reg"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "---- Linear Regression - Model Evaluation ----\n",
      "Mean Absolute Error (MAE): 12.124047253954059\n",
      "Mean Squared Error (MSE): 251.3233102059905\n",
      "Root Mean Squared Error (RMSE): 15.853179813715307\n",
      "R2 Score : 0.7519520798394908\n"
     ]
    }
   ],
   "source": [
    "from sklearn.linear_model import LinearRegression\n",
    "linear_regressor = LinearRegression()\n",
    "linear_regressor.fit(X_train,y_train)\n",
    "y_pred_lr = linear_regressor.predict(X_test)\n",
    "\n",
    "\n",
    "print(\"---- Linear Regression - Model Evaluation ----\")\n",
    "print(\"Mean Absolute Error (MAE): {}\".format(mae(y_test, y_pred_lr)))\n",
    "print(\"Mean Squared Error (MSE): {}\".format(mse(y_test, y_pred_lr)))\n",
    "print(\"Root Mean Squared Error (RMSE): {}\".format(np.sqrt(mse(y_test, y_pred_lr))))\n",
    "print(\"R2 Score : {}\".format(r2_score(y_test, y_pred_lr)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "---- Linear Regression - Model Evaluation ----\n",
      "Mean Absolute Error (MAE): 13.780087532997358\n",
      "Mean Squared Error (MSE): 329.6670352713083\n",
      "Root Mean Squared Error (RMSE): 18.15673525916232\n",
      "R2 Score : 0.6746293752954868\n"
     ]
    }
   ],
   "source": [
    "from sklearn.ensemble import RandomForestRegressor\n",
    "randomforest_regressor = RandomForestRegressor(100)\n",
    "randomforest_regressor.fit(X_train,y_train)\n",
    "y_pred_rf = randomforest_regressor.predict(X_test)\n",
    "\n",
    "\n",
    "print(\"---- Linear Regression - Model Evaluation ----\")\n",
    "print(\"Mean Absolute Error (MAE): {}\".format(mae(y_test, y_pred_rf)))\n",
    "print(\"Mean Squared Error (MSE): {}\".format(mse(y_test, y_pred_rf)))\n",
    "print(\"Root Mean Squared Error (RMSE): {}\".format(np.sqrt(mse(y_test, y_pred_rf))))\n",
    "print(\"R2 Score : {}\".format(r2_score(y_test, y_pred_rf)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "---- Linear Regression - Model Evaluation ----\n",
      "Mean Absolute Error (MAE): 16.95680345572354\n",
      "Mean Squared Error (MSE): 521.9805615550756\n",
      "Root Mean Squared Error (RMSE): 22.846893914820797\n",
      "R2 Score : 0.48482218958587875\n"
     ]
    }
   ],
   "source": [
    "from sklearn.tree import DecisionTreeRegressor\n",
    "decisiontree_regressor = DecisionTreeRegressor()\n",
    "decisiontree_regressor.fit(X_train,y_train)\n",
    "y_pred_dt = decisiontree_regressor.predict(X_test)\n",
    "\n",
    "\n",
    "print(\"---- Linear Regression - Model Evaluation ----\")\n",
    "print(\"Mean Absolute Error (MAE): {}\".format(mae(y_test, y_pred_dt)))\n",
    "print(\"Mean Squared Error (MSE): {}\".format(mse(y_test, y_pred_dt)))\n",
    "print(\"Root Mean Squared Error (RMSE): {}\".format(np.sqrt(mse(y_test, y_pred_dt))))\n",
    "print(\"R2 Score : {}\".format(r2_score(y_test, y_pred_dt)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "---- Linear Regression - Model Evaluation ----\n",
      "Mean Absolute Error (MAE): 12.174426150888397\n",
      "Mean Squared Error (MSE): 247.64570208764022\n",
      "Root Mean Squared Error (RMSE): 15.73676275755723\n",
      "R2 Score : 0.7555817592519357\n"
     ]
    }
   ],
   "source": [
    "from sklearn.ensemble import AdaBoostRegressor\n",
    "adaboost_regressor = AdaBoostRegressor(base_estimator=linear_regressor, n_estimators=100)\n",
    "adaboost_regressor.fit(X_train,y_train)\n",
    "y_pred_ab = adaboost_regressor.predict(X_test)\n",
    "\n",
    "\n",
    "print(\"---- Linear Regression - Model Evaluation ----\")\n",
    "print(\"Mean Absolute Error (MAE): {}\".format(mae(y_test, y_pred_ab)))\n",
    "print(\"Mean Squared Error (MSE): {}\".format(mse(y_test, y_pred_ab)))\n",
    "print(\"Root Mean Squared Error (RMSE): {}\".format(np.sqrt(mse(y_test, y_pred_ab))))\n",
    "print(\"R2 Score : {}\".format(r2_score(y_test, y_pred_ab)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pickle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [],
   "source": [
    "filename = 'model.pkl'\n",
    "pickle.dump(linear_regressor, open(filename, 'wb'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
